package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.SysDictDataVO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.api.service.CcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysDictDataService;

/**
 * 字典 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService
{
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private CcService ccService;

    /**
     * 根据条件分页查询字典数据
     * 
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData)
    {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型查询字典数据
     * 
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType)
    {
        return dictDataMapper.selectDictDataByType(dictType);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictDataVO> selectDictDataSampleByType(String dictType)
    {
        List<SysDictDataVO> sysDictDataVOList = new ArrayList<>();
        List<SysDictData> sysDictDataList = dictDataMapper.selectDictDataByType(dictType);
        if(sysDictDataList!=null){
            for (SysDictData sysDictData : sysDictDataList) {
                SysDictDataVO sysDictDataVO = new SysDictDataVO();
                sysDictDataVO.setDictLabel(sysDictData.getDictLabel());
                sysDictDataVO.setDictValue(sysDictData.getDictValue());

                sysDictDataVOList.add(sysDictDataVO);
            }
        }

        return sysDictDataVOList;
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     * 
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 通过字典ID删除字典数据信息
     * 
     * @param dictCode 字典数据ID
     * @return 结果
     */
    @Override
    public int deleteDictDataById(Long dictCode)
    {
        return dictDataMapper.deleteDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据
     * 
     * @param ids 需要删除的数据
     * @return 结果
     */
    @Override
    public int deleteDictDataByIds(String ids)
    {
        return dictDataMapper.deleteDictDataByIds(Convert.toStrArray(ids));
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData dictData)
    {
        return dictDataMapper.insertDictData(dictData);
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData dictData)
    {
        return dictDataMapper.updateDictData(dictData);
    }

    @Override
    public List selectHotAccountType(Long engineerId) {

        String response = ccService.getParamTables1("hot_account_type", engineerId);

        if (response == null) {
            return null;
        }

        try {
            JSONObject jsonObject = JSONObject.parseObject(response);
            if ("1".equals(jsonObject.get("status"))) {
                JSONArray array = jsonObject.getJSONArray("record");
                if (null != array) {
                    List<Map<String, String>> results = new ArrayList<>();
                    for (int i = 0; i < array.size(); i++) {
                        boolean is_disabled = array.getJSONObject(i).getBooleanValue("is_disabled");
                        if (!is_disabled) {
                            Map<String, String> result = new HashMap<>();
                            result.put("value", array.getJSONObject(i).getString("account_id"));
                            result.put("text", array.getJSONObject(i).getString("account_name"));
                            results.add(result);
                        }
                    }
                    return results;
                }
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    @Override
    public List selectCCFailtype(Long engineerId) {
        String response = ccService.getParamTables1("fail_type", engineerId);

        if (response == null) {
            return null;
        }

        try {
            JSONObject jsonObject = JSONObject.parseObject(response);
            if ("1".equals(jsonObject.get("status"))) {
                JSONArray array = jsonObject.getJSONArray("record");
                if (null != array) {
                    List<Map<String, String>> results = new ArrayList<>();
                    for (int i = 0; i < array.size(); i++) {
                        boolean is_disabled = array.getJSONObject(i).getBooleanValue("is_disabled");
                        if (!is_disabled) {
                            Map<String, String> result = new HashMap<>();
                            result.put("value", array.getJSONObject(i).getString("fail_id"));
                            result.put("text", array.getJSONObject(i).getString("fail_name"));
                            results.add(result);
                        }
                    }
                    return results;
                }
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    @Override
    public List selectCCEquipmentModel(Long engineerId) {
        String response = ccService.getParamTables1("equipment_model", engineerId);

        if (response == null) {
            return null;
        }

        try {
            JSONObject jsonObject = JSONObject.parseObject(response);
            if ("1".equals(jsonObject.get("status"))) {
                JSONArray array = jsonObject.getJSONArray("record");
                if (null != array) {
                    List<Map<String, String>> results = new ArrayList<>();
                    for (int i = 0; i < array.size(); i++) {
                        boolean is_disabled = array.getJSONObject(i).getBooleanValue("is_disabled");
                        if (!is_disabled) {
                            Map<String, String> result = new HashMap<>();
                            result.put("value", array.getJSONObject(i).getString("equipment_id"));
                            result.put("text", array.getJSONObject(i).getString("equipment_name"));
                            results.add(result);
                        }
                    }
                    return results;
                }
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    @Override
    public List selectCCInstrumentStatus(Long engineerId) {
        String response = ccService.getParamTables1("HTEquiStatus", engineerId);

        if (response == null) {
            return null;
        }

        try {
            JSONObject jsonObject = JSONObject.parseObject(response);
            if ("1".equals(jsonObject.get("status"))) {
                JSONArray array = jsonObject.getJSONArray("record");
                if (null != array) {
                    List<Map<String, String>> results = new ArrayList<>();
                    for (int i = 0; i < array.size(); i++) {
                        boolean is_disabled = array.getJSONObject(i).getBooleanValue("is_disabled");

                        if (!is_disabled) {
                            Map<String, String> result = new HashMap<>();
                            result.put("value", array.getJSONObject(i).getString("id"));
                            result.put("text", array.getJSONObject(i).getString("name"));
                            results.add(result);
                        }
                    }
                    return results;
                }
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    @Override
    public List selectCCHTSeverity(Long engineerId) {
        String response = ccService.getParamTables1("HTSeverity", engineerId);

        if (response == null) {
            return null;
        }

        try {
            JSONObject jsonObject = JSONObject.parseObject(response);
            if ("1".equals(jsonObject.get("status"))) {
                JSONArray array = jsonObject.getJSONArray("record");
                if (null != array) {
                    List<Map<String, String>> results = new ArrayList<>();
                    for (int i = 0; i < array.size(); i++) {
                        boolean is_disabled = array.getJSONObject(i).getBooleanValue("is_disabled");
                        if (!is_disabled) {
                            Map<String, String> result = new HashMap<>();
                            result.put("value", array.getJSONObject(i).getString("id"));
                            result.put("text", array.getJSONObject(i).getString("name"));
                            results.add(result);
                        }
                    }
                    return results;
                }
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }
}
